package com.woniuxy.service.impl;
/*
 * @author 幸琦林
 *
 */

import com.woniuxy.dao.CourseInfo;
import com.woniuxy.entity.Course;
import com.woniuxy.entity.ShowCourse;
import com.woniuxy.service.CourseInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;

@Service
public class CourseInfoServiceImpl implements CourseInfoService {

    @Resource
    CourseInfo courseInfo;

    @Override
    public ShowCourse[][] getShowCourse(Long gymId) {
        List<ShowCourse> allCourse = courseInfo.getShowCourse(gymId);
        ShowCourse[][] courses = new ShowCourse[7][6];
        Calendar ca = Calendar.getInstance();

        for (ShowCourse course : allCourse) {
            ca.setTime(course.getDate());
            courses[ca.get(Calendar.DAY_OF_WEEK) - 1][course.getTime() - 1] = course;
        }
        return courses;
    }

    /**
     * 添加课程
     * 返回2表示该编辑者已经添加过同一名称的课程
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
	public int addCourse(long id, String name, double price) {
		//判断该编辑者有没有添加过该课程
		if(courseInfo.getCourseByEditorIdAndCourseName(id,name)!=null) {
            return 3;
        }
        //添加新课程
        Course newCourse = new Course();
        newCourse.setEditorId(id);
        newCourse.setCourseName(name);
        newCourse.setPrice(price);
        courseInfo.addCourse(newCourse);
        return 1;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int choseCourse(Long courseId, Long userId) {
        return courseInfo.choseCourse(courseId, userId);
    }
}
